#include "home.h"
#include "ui_home.h"

Home::Home(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::home)
{
//    setWindowFlags(Qt::FramelessWindowHint);
    ui->setupUi(this);

    my_tcpServer = new Tcpserver();


    //启动服务器线程
    my_tcpServer->listen(QHostAddress::Any,6666);

//    Dev_send *t1 = new Dev_send(my_tcpServer,client);
//    t1->start();

    qDebug() << "服务器等待新的客户端连接";
    connect(my_tcpServer,&QTcpServer::newConnection,this,&Home::newClientHandler);

    /*启动定时器*/
    my_timer =  new QTimer(this);
    connect(my_timer,SIGNAL(timeout()),this,SLOT(slot_timeout()));
    my_timer->start(1000);



    /*空调窗口*/
    wid_DeviceAC = new DeviceAC(LIVINGROOM,this);
    connect(wid_DeviceAC,SIGNAL(sig_sendData(int,QString)),my_tcpServer,SLOT(slot_deviceSendData(int,QString)));
    wid_DeviceAC->move(320,150);

    /*百叶窗窗口*/
    wid_DeviceCurtain = new DeviceCurtain(LIVINGROOM,this);
    connect(wid_DeviceCurtain,SIGNAL(sig_sendData(int,QString)),my_tcpServer,SLOT(slot_deviceSendData(int,QString)));
    wid_DeviceCurtain->move(680,150);

    /*吊灯窗口*/
    wid_DeviceLamp = new DeviceLamp(LIVINGROOM,this);
    connect(wid_DeviceLamp,SIGNAL(sig_sendData(int,QString)),my_tcpServer,SLOT(slot_deviceSendData(int,QString)));
    wid_DeviceLamp->move(20,150);

    /*风扇窗口*/
    wid_DeviceFan = new DeviceFan(LIVINGROOM,this);
    connect(wid_DeviceFan,SIGNAL(sig_sendData(int,QString)),my_tcpServer,SLOT(slot_deviceSendData(int,QString)));
    wid_DeviceFan->move(20,360);



    /* 设置样式*/
    QPalette pal = this->palette();
    pal.setBrush(QPalette::Background,QBrush(QPixmap("://png/背景.png")));
    setPalette(pal);



    ui->label_keting->setStyleSheet("color:#FFFFFF");
    ui->label_time_1->setStyleSheet("color:#FFFFFF");
    ui->label_time_2->setStyleSheet("color:#FFFFFF");

     ui->label_fenge->setFixedWidth(3);
     ui->label_fenge->setText("");
    ui->label_fenge->setStyleSheet("background-color:#FFFFFF");



    /*获取时间*/
    ui->label_time_1->setText(QTime::currentTime().toString("hh:mm"));
    ui->label_time_2->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd ddd"));

}

void Home::slot_timeout(void)
{
    /*获取时间*/
    ui->label_time_1->setText(QTime::currentTime().toString("hh:mm"));
    ui->label_time_2->setText(QDateTime::currentDateTime().toString("yyyy-MM-dd ddd"));

}

void Home::newClientHandler()
{
    qDebug() << "服务器接收到一个新的客户端连接";
    QTcpSocket *client = my_tcpServer->nextPendingConnection();
    qDebug() << "client: "<< client;
    Dev_send *t1 = new Dev_send(my_tcpServer,client);
    t1->start();

    // 启动线程
    Tcpserver_Thread *t = new Tcpserver_Thread(client);
    t->start();


}



Home::~Home()
{
    delete ui;
}
